<?php

/**
 * This is the model class for table "milksales".
 *
 * The followings are the available columns in table 'milksales':
 * @property string $id
 * @property string $saleDate
 * @property string $isDay
 * @property string $glId
 * @property string $contractedQuantity
 * @property string $contractedQuantityRent
 * @property string $mandiQuantity
 * @property string $mandiQuantityRent
 *
 * The followings are the available model relations:
 * @property Gl $gl
 */
class MilkSales extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'milksales';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('saleDate, glId', 'required'),
			array('id, isDay', 'length', 'max'=>1),
			array('glId', 'length', 'max'=>11),
			array('contractedQuantity, contractedQuantityRent, mandiQuantity, mandiQuantityRent', 'length', 'max'=>5),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, saleDate, isDay, glId, contractedQuantity, contractedQuantityRent, mandiQuantity, mandiQuantityRent', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'gl' => array(self::BELONGS_TO, 'Gl', 'glId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'saleDate' => 'Sale Date',
			'isDay' => 'Is Day',
			'glId' => 'Gl',
			'contractedQuantity' => 'Contracted Quantity',
			'contractedQuantityRent' => 'Contracted Quantity Rent',
			'mandiQuantity' => 'Mandi Quantity',
			'mandiQuantityRent' => 'Mandi Quantity Rent',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('saleDate',$this->saleDate,true);
		$criteria->compare('isDay',$this->isDay,true);
		$criteria->compare('glId',$this->glId,true);
		$criteria->compare('contractedQuantity',$this->contractedQuantity,true);
		$criteria->compare('contractedQuantityRent',$this->contractedQuantityRent,true);
		$criteria->compare('mandiQuantity',$this->mandiQuantity,true);
		$criteria->compare('mandiQuantityRent',$this->mandiQuantityRent,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return MilkSales the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	protected function beforeSave(){
		if(parent::beforeSave())
		{
			$temp = date('d-m-Y', CDateTimeParser::parse($this->saleDate, 'dd-MM-yyyy'));
			if($this->saleDate == $temp)
				$this->saleDate = date('Y-m-d', CDateTimeParser::parse($this->saleDate, 'dd-MM-yyyy'));
			// echo $this->saleDate; exit;
			return TRUE;
		}
		else 
			return FALSE;
	}
}
